//
//  WeChatStylePlaceHolder.m
//  CYLTableViewPlaceHolder
//
//  Created by 陈宜龙 on 15/12/23.
//  Copyright © 2015年 http://weibo.com/luohanchenyilong/ ÂæÆÂçö@iOSÁ®ãÂ∫èÁä≠Ë¢Å. All rights reserved.
//

static float const kUIemptyOverlayLabelX         = 0;
static float const kUIemptyOverlayLabelY         = 0;
static float const kUIemptyOverlayLabelHeight    = 20;

#import "WeChatStylePlaceHolder.h"

@interface WeChatStylePlaceHolder ()

@property (nonatomic, strong) UIImageView *emptyOverlayImageView;

@end

@implementation WeChatStylePlaceHolder

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self = [self sharedInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self = [self sharedInit];
    }
    return self;
}

- (instancetype)sharedInit {
    self.backgroundColor = RGBColor(240, 240, 240);
    self.backgroundColor = RGBColor(240, 240, 240);
    self.contentMode =   UIViewContentModeTop;
    [self addUIemptyOverlayImageView];
//    [self addUIemptyOverlayLabel];
//    [self setupUIemptyOverlay];
    return self;
}


- (void)addUIemptyOverlayImageView {
    UIImage *image = [UIImage imageNamed:@"暂无数据~"];
    self.emptyOverlayImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    self.emptyOverlayImageView.center = CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2 - 50);
    self.emptyOverlayImageView.image = image;
    [self addSubview:self.emptyOverlayImageView];
}



- (void)addUIemptyOverlayLabel {
    CGRect emptyOverlayViewFrame = CGRectMake(kUIemptyOverlayLabelX, kUIemptyOverlayLabelY,150, 40);
    UIButton *emptyOverlayBtn = [[UIButton alloc] initWithFrame:emptyOverlayViewFrame];
    [emptyOverlayBtn setTitle:@"点我重新加载" forState:UIControlStateNormal];
    [emptyOverlayBtn setTitleColor:RGBColor(0, 145, 217) forState:UIControlStateNormal];
    emptyOverlayBtn.adjustsImageWhenHighlighted = NO;
    [emptyOverlayBtn addTarget:self action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
    emptyOverlayBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    emptyOverlayBtn.frame = ({
        CGRect frame = emptyOverlayBtn.frame;
        frame.origin.y = CGRectGetMaxY(self.emptyOverlayImageView.frame) + 10;
        frame.origin.x = CGRectGetMidX(self.emptyOverlayImageView.frame) - 75;
        frame;
    });
    emptyOverlayBtn.layer.borderColor = RGBColor(0, 145, 217).CGColor;
    emptyOverlayBtn.layer.borderWidth = 0.5;
    emptyOverlayBtn.layer.cornerRadius = 5;
    [self addSubview:emptyOverlayBtn];
}

- (void)setupUIemptyOverlay {
    //    self.userInteractionEnabled = YES;
    //    UILongPressGestureRecognizer *longPressUIemptyOverlay = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressUIemptyOverlay:)];
    //    [longPressUIemptyOverlay setMinimumPressDuration:0.001];
    //    [self addGestureRecognizer:longPressUIemptyOverlay];
    //    self.userInteractionEnabled = YES;
}

- (void)longPressUIemptyOverlay:(UILongPressGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        self.emptyOverlayImageView.alpha = 0.4;
    }
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        self.emptyOverlayImageView.alpha = 1;
        if ([self.delegate respondsToSelector:@selector(emptyOverlayClicked:)]) {
            [self.delegate emptyOverlayClicked:nil];
        }
    }
}

- (void)reload {
    if ([self.delegate respondsToSelector:@selector(emptyOverlayClicked:)]) {
        [self.delegate emptyOverlayClicked:nil];
    }
}

@end
